package demo3;

/**
 * synchronized锁定静态方法
 */
public class T {

    private static int count = 10;

    public synchronized static void m() {
        count--;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("%s count = %d%n", Thread.currentThread().getName(), count);
    }

    public static void mm() {
        synchronized (T.class) { //T.class是Class中的一个对象，这里是不能用synchronized(this)的,因为静态方法是不需要new对象去访问的
            count--;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("%s count = %d%n", Thread.currentThread().getName(), count);
        }

    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(T::m);
        Thread thread2 = new Thread(T::m);
        thread1.start();
        thread2.start();
    }

}
